from flask import g

from util.db_util import connect


# 登录
def user_login(username):
    conn = connect()
    try:
        with conn.cursor() as cursor:
            sql = "SELECT * FROM user WHERE user_name = %s"
            cursor.execute(sql, username)
            result = cursor.fetchone()
            if result:
                # 转成字典
                result = dict(result)
                return result
            return None
    finally:
        conn.close()


# 注册用户
def user_register(username, password):
    conn = connect()
    try:
        with conn.cursor() as cursor:
            sql = "INSERT INTO user (user_name, password) VALUES (%s, %s)"
            cursor.execute(sql, (username, password))
            conn.commit()
            return cursor.lastrowid

    finally:
        conn.close()


# 查询用户信息
def query_user_info():
    conn = connect()
    try:
        with conn.cursor() as cursor:
            sql = "SELECT user_name,password FROM user WHERE user_id = %s"
            cursor.execute(sql, (g.user['id'],))
            result = cursor.fetchone()
            if result:
                # 转成字典
                result = dict(result)
                return result
            return None
    finally:
        conn.close()

# 修改用户信息
def update_user_info(username, password):
    conn = connect()
    try:
        with conn.cursor() as cursor:
            sql = "UPDATE user SET user_name = %s, password = %s WHERE user_id = %s"
            cursor.execute(sql, (username, password, g.user['id']))
            conn.commit()
            return cursor.lastrowid
    finally:
        conn.close()

#查询选中的宝宝
def query_baby_choose():
    conn = connect()
    try:
        with conn.cursor() as cursor:
            sql = "SELECT choose_baby FROM user WHERE user_id = %s"
            cursor.execute(sql, (g.user['id'],))
            result = cursor.fetchone()
            if result:
                return result['choose_baby']
    finally:
        conn.close()

# 查询选中宝宝名称
def query_baby_choose_name():
    conn = connect()
    choose = query_baby_choose()
    try:
        with conn.cursor() as cursor:
            sql = "SELECT name FROM baby WHERE id = %s"
            cursor.execute(sql, (choose,))
            result = cursor.fetchone()
            if result:
                return result['name']
    finally:
        conn.close()

# 更新选中宝宝
def update_baby_choose(baby_id):
    conn = connect()
    try:
        with conn.cursor() as cursor:
            sql = "UPDATE user SET choose_baby = %s WHERE user_id = %s"
            cursor.execute(sql, (baby_id, g.user['id']))
            conn.commit()
            return cursor.lastrowid
    finally:
        conn.close()

# 查询提醒方式
def query_msg_type():
    conn = connect()
    try:
        with conn.cursor() as cursor:
            sql = "SELECT msg_type FROM user WHERE user_id = %s"
            cursor.execute(sql, (g.user['id'],))
            result = cursor.fetchone()
            if result:
                return result['msg_type']
            return None
    finally:
        conn.close()

#设置提醒方式
def update_msg_type(msg_type):
    conn = connect()
    try:
        with conn.cursor() as cursor:
            sql = "UPDATE user SET msg_type = %s WHERE user_id = %s"
            cursor.execute(sql, (msg_type,g.user['id']))
            conn.commit()
            return cursor.lastrowid
    finally:
        conn.close()

# 用户名查重
def user_name_check(username):
    conn = connect()
    try:
        with conn.cursor() as cursor:
            sql = "SELECT * FROM user WHERE user_name = %s"
            cursor.execute(sql, username)
            result = cursor.fetchone()
            if result:
                return True
            return False
    finally:
        conn.close()

# 查询绑定手机
def query_phone():
    conn = connect()
    try:
        with conn.cursor() as cursor:
            sql = "SELECT phone FROM user WHERE user_id = %s"
            cursor.execute(sql, (g.user['id'],))
            result = cursor.fetchone()
            if result:
                return result['phone']
            return None
    finally:
        conn.close()

# 查询绑定邮箱
def query_email():
    conn = connect()
    try:
        with conn.cursor() as cursor:
            sql = "SELECT mailbox FROM user WHERE user_id = %s"
            cursor.execute(sql, (g.user['id'],))
            result = cursor.fetchone()
            if result:
                return result['mailbox']
            return None
    finally:
        conn.close()
# 绑定手机
def set_phone(phone):
     conn = connect()
     try:
         with conn.cursor() as cursor:
             sql = "UPDATE user SET phone = %s WHERE user_id = %s"
             cursor.execute(sql, (phone,g.user['id']))
             conn.commit()
             return cursor.lastrowid
     finally:
         conn.close()

# 绑定邮箱
def set_email(email):
     conn = connect()
     try:
         with conn.cursor() as cursor:
             sql = "UPDATE user SET mailbox = %s WHERE user_id = %s"
             cursor.execute(sql, (email,g.user['id']))
             conn.commit()
             return cursor.lastrowid
     finally:
         conn.close()